# 编译生成文件统一存放至buil目录
import os
from os import path
import config
from config import logger

DefaultEnvironment(tools=[])

env = Environment(tools = ['mingw'],
    AS = config.AS, ASFLAGS = config.AFLAGS,
    CC = config.CC, CFLAGS = config.CFLAGS,
    AR = config.AR, ARFLAGS = '-rc',
    CXX = config.CXX, CXXFLAGS = config.CXXFLAGS,
    LINK = config.LINK, LINKFLAGS = config.LFLAGS)
env.PrependENVPath('PATH', config.EXEC_PATH)

env.Replace(
    ARCOMSTR = 'AR $SOURCE',
    ASCOMSTR = 'AS $SOURCE',
    ASPPCOMSTR = 'AS $SOURCE',
    CCCOMSTR = 'CC $SOURCE',
    CXXCOMSTR = 'CXX $SOURCE',
    LINKCOMSTR = 'LINK $TARGET,FLAGS =$LINKFLAGS'
)

logger.info('ARCH       = %s' %(config.ARCH      ))
logger.info('CPU        = %s' %(config.CPU       ))
logger.info('CROSS_TOOL = %s' %(config.CROSS_TOOL))
logger.info('PLATFORM   = %s' %(config.PLATFORM  ))
logger.info('GCC_ROOT   = %s' %(config.EXEC_PATH ))
logger.info('CC         = %s' %(config.CC        ))
logger.info('AS         = %s' %(config.AS        ))
logger.info('AR         = %s' %(config.AR        ))
logger.info('LINK       = %s' %(config.LINK      ))
logger.info('LINKSCRIPT = %s' %(config.LINKSCRIPT))
logger.info('ROOT_PATH  = %s' %(config.ROOT_PATH ))
logger.info('BUILD_PATH = %s' %(config.BUILD_PATH ))

cwd = os.getcwd()
logger.info(os.curdir)
env.Append(CPPDEFINES = [
            'USE_HAL_DRIVER',
            'STM32F407xx',
            ])
env.Append(CPPPATH = [
           path.join(cwd,'Inc'),
           path.join(cwd,'Drivers/STM32F4xx_HAL_Driver/Inc'),
           path.join(cwd,'Drivers/STM32F4xx_HAL_Driver/Inc/Legacy'),
           path.join(cwd,'Drivers/CMSIS/Device/ST/STM32F4xx/Include'),
           path.join(cwd,'Drivers/CMSIS/Include')
           ])


Export('env')
Export('config')

s = config.getTargetName(config.BUILD_PATH,'env')

# SConscript('SConscript','env',variant_dir = 'build', duplicate=False)

# VariantDir('build','Src',duplicate=0)
# SConscript('build/Sconscript') # 这里应当指定生成目录下的SConscript文件

# env.VariantDir('build/Src','Src',duplicate=0)
# env.VariantDir('build/Drivers','Drivers',duplicate=0)

# env.VariantDir('build', 'Src', duplicate=0)

srcs = Glob('*.c') + Glob('*.s')

objs = env.Object(srcs)

objs += SConscript('Src/SConscript')
objs += SConscript('Drivers/SConscript')

env.Program(path.join(config.BUILD_PATH,config.TARGET), objs)

env.AddPostAction(path.join(config.BUILD_PATH,config.TARGET), config.POST_ACTION)






